{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "pleased-picking",
   "metadata": {},
   "source": [
    "# GeoMap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exterior-estonia",
   "metadata": {},
   "source": [
    "https://data.opendatasoft.com/explore/dataset/european-union-countries@public/export/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "mounted-thought",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import plotly.express as px\n",
    "import plotly.graph_objects as go\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "editorial-enough",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.io as pio\n",
    "pio.renderers.default = 'browser'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "behavioral-karma",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoplot = pd.read_csv(\"food_supply_original.csv\")\n",
    "european_union = json.load(open(\"european-union-countries.geojson\", \"r\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "christian-russian",
   "metadata": {},
   "outputs": [],
   "source": [
    "# check properties\n",
    "# european_union[\"features\"][1][\"properties\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "emerging-indie",
   "metadata": {},
   "outputs": [],
   "source": [
    "smo = pd.read_csv(\"share-of-adults-who-smoke.csv\")\n",
    "#retrieved from https://ourworldindata.org/smoking\n",
    "smo = smo[smo[\"Year\"] == 2016].rename(columns={\"Code\":\"iso_a3\", \"Smoking prevalence, total (ages 15+)\":\"Smoking\"})\n",
    "smo = smo[[\"iso_a3\", \"Smoking\"]]\n",
    "smo.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cellular-fellow",
   "metadata": {},
   "source": [
    "## Preprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abandoned-detail",
   "metadata": {},
   "outputs": [],
   "source": [
    "state_id_map = {}\n",
    "for feature in european_union[\"features\"]:\n",
    "    feature[\"id\"] = feature[\"properties\"][\"gu_a3\"]\n",
    "    state_id_map[feature[\"properties\"][\"name_long\"]] = feature[\"id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "connected-nutrition",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create dict with necessary information \n",
    "state_id_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "nutritional-excuse",
   "metadata": {},
   "outputs": [],
   "source": [
    "# rename column and value in column cause doesn't match \n",
    "geoplot.rename(columns={\"Unnamed: 0\": \"Country\"}, inplace=True)\n",
    "geoplot[\"Country\"].replace({\"Czechia\": \"Czech Republic\"}, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "representative-colonial",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create new column with polygon info\n",
    "geoplot[\"iso_a3\"] = geoplot[\"Country\"].apply(lambda x: state_id_map[x])\n",
    "geoplot.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "qualified-library",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoplot = geoplot.merge(smo, on=\"iso_a3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bottom-questionnaire",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoplot = geoplot[['Country', 'Milk - Excluding Butter', 'Oilcrops', 'Eggs',\n",
    "       'Starchy Roots', 'Meat', 'Vegetables', 'Fruits - Excluding Wine',\n",
    "       'Aquatic Products, Other', 'Treenuts', 'Spices', 'Pulses',\n",
    "       'Alcoholic Beverages', 'Cereals - Excluding Beer', 'Animal fats',\n",
    "       'Vegetable Oils', 'Offals', 'Sugar & Sweeteners', 'Fish, Seafood',\n",
    "       'Stimulants', 'Smoking', 'Obesity', 'total_cases_per_million',\n",
    "       'total_deaths_per_million', 'life_expectancy',\n",
    "       'human_development_index', 'population_density', 'median_age',\n",
    "       'gdp_per_capita', 'cardiovasc_death_rate',\n",
    "       'Alcoholic Beverages above avg', 'Vegetables above avg',\n",
    "       'Obesity above avg', 'iso_a3']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "psychological-remains",
   "metadata": {},
   "outputs": [],
   "source": [
    "geoplot.to_csv(\"food_supply.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "civil-avenue",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "flexible-essex",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = px.choropleth_mapbox(\n",
    "    geoplot,\n",
    "    locations=\"iso_a3\",\n",
    "    geojson=european_union,\n",
    "    color=\"Smoking\",\n",
    "    hover_name=\"Country\",\n",
    "    hover_data=[\"cardiovasc_death_rate\"],\n",
    "    #title=\"Life expectancy Europe\",\n",
    "    mapbox_style=\"carto-positron\", # kann hier auch darkmode u.a. - mit api token noch mehr aber to much\n",
    "    center={\"lat\": 56.5, \"lon\": 11},\n",
    "    zoom=2.5,\n",
    "    opacity=0.5, # können das hier weniger transparent machen\n",
    "    height=600,\n",
    "    width=550\n",
    ")\n",
    "fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesbian-process",
   "metadata": {},
   "source": [
    "## For Dash"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stainless-shade",
   "metadata": {},
   "source": [
    "Nicht ganz dasselbe weil kein token aber bestimmt übetragbar: \n",
    "https://plotly.com/python/mapbox-county-choropleth/\n",
    "\n",
    "Filter mit variablen dann in dash welche wir für das coloring wollen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "billion-attraction",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "auburn-mills",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "single-crisis",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "heated-understanding",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "minus-bidder",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "undefined-feedback",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "absolute-communist",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lightweight-disability",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "foreign-reviewer",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "naughty-syndicate",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "brief-cycling",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amazing-somewhere",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wired-skill",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
